<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class admin extends CI_Controller {
	
	private $id;
	private $password;
	
	public function __construct() {
		
		parent::__construct();
		
		//$this->output->enable_profiler(TRUE);
		$this->load->config('admin');
			
		$this->id = $this->config->item('id');
		$this->password = $this->config->item('password');
		
		if(METHOD != 'index' && METHOD != 'login_action') {
			if($this->id != $this->session->userdata('admin_id')) header('Location: '.base_url('admin'));
		} 
		
		$this->load->model('admin_model');
	}
	
	public function index() {
		
		$this->load->view('admin/login');
		
	}
	
	public function test() {
	
		$this->load->view('admin/test');
	
	}

	public function login_action() {
		
		$id = $this->input->post('id');
		$password = $this->input->post('password');

		if ($this->id == $id && $this->password == $password) {
			$result = true;

			$newdata = array(
					'admin_id' => $id
			);
			
			$this->session->set_userdata($newdata);
		}
		else $result = false;

		$json = json_encode(array('result' => $result)); 
		
		$data['json'] = $json;

		$this->load->view('json', $data);
		
	}
	
	public function ckeditor_image_upload() {
		
		$funcNum = $this->input->get('CKEditorFuncNum');
		$CKEditor = $this->input->get('CKEditor');
		$langCode = $this->input->get('langCode');
		
		$this->load->library('upload');
		
		$config['upload_path'] = UPLOAD_PATH;
		$config['allowed_types'] = 'gif|jpg|png';
		
		
		$this->upload->initialize($config);
		
		$this->upload->do_upload('upload');
		if($this->upload->display_errors()) {
			$url = '';
			$message = $this->upload->display_errors('','');
		} else {
			$image = $this->upload->data();
			$url = UPLOAD_DIR.'/'.$image['file_name'];
			$message = '';
		}
		
		$html ='<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction('.$funcNum.', "'.$url.'", "'.$message.'");</script>';
			
		$data['html'] = $html;
			
		$this->load->view('html', $data);
	}
	
	public function pageview($limit = 10, $offset = 0) {
		
		$pageview_rows = $this->admin_model->pageview($limit, $offset);
		
		$total_rows = $this->admin_model->pageview_total_rows();
		
		$this->load->library('pagination');
		
		$config['base_url'] = base_url(CONTROLLER.'/'.METHOD).'/'.$limit;
		$config['total_rows'] = $total_rows;
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><span>';
		$config['cur_tag_close'] = '</span></li>';
		$config['first_link'] = FALSE;
		$config['last_link'] = FALSE;
		$config['prev_link'] = 'Prev';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		$config['next_link'] = 'Next';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		
		$this->pagination->initialize($config);
		
		$pagination_links =  $this->pagination->create_links();
		
		$data['limit'] =  $limit;
		$data['pageview_rows'] =  $pageview_rows;
		$data['pagination_links'] =  $pagination_links;
		
		$this->load->view('admin/pageview', $data);
		
	}
	
	public function cover($limit = 10, $offset = 0) {
		
		$cover_rows = $this->admin_model->cover($limit, $offset);
		
		$total_rows = $this->admin_model->cover_total_rows();
		
		$sort_array = array();
		$cover_rows_array = array();
		foreach($cover_rows as $row) {
			array_push($sort_array, $row['sort']);
			
			$image = json_decode($row['image'], true);
			$row['image_count'] = count($image);
			if(!empty($row['image_count'])) {
				$row['image_is_image'] = $image[0]['is_image'];
				$row['image_file_name'] = $image[0]['file_name'];
				$row['image_orig_name'] = $image[0]['orig_name'];
			} else {
				$row['image_is_image'] = 0;
				$row['image_file_name'] = ';';
				$row['image_orig_name'] = '&nbsp;';
			}
			array_push($cover_rows_array, $row);
		}
		$sort_string = implode(',', $sort_array);
		
		$this->load->library('pagination');
		
		$config['base_url'] = base_url(CONTROLLER.'/'.METHOD).'/'.$limit;
		$config['total_rows'] = $total_rows;
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><span>';
		$config['cur_tag_close'] = '</span></li>';
		$config['first_link'] = FALSE;
		$config['last_link'] = FALSE;
		$config['prev_link'] = 'Prev';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		$config['next_link'] = 'Next';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		
		$this->pagination->initialize($config);
		
		$pagination_links =  $this->pagination->create_links();
		
		$data['limit'] =  $limit;
		$data['offset'] =  $offset;
		$data['cover_rows'] =  $cover_rows_array;
		$data['sort_string'] =  $sort_string;
		$data['pagination_links'] =  $pagination_links;
		
		$this->load->view('admin/cover', $data);
	}
	
	public function cover_write($limit, $offset) {
	
		$data['limit'] =  $limit;
		$data['offset'] =  $offset;
		
		$this->load->view('admin/cover_write', $data);
	}
	
	public function cover_write_action() {
		
		$title = $this->input->post('title');
		$date = $this->input->post('date');
		$font_type = $this->input->post('font_type');
		$font_color = $this->input->post('font_color');
		$thumb_view = $this->input->post('thumb_view');
		$type = $this->input->post('type');
		$link = $this->input->post('link');
		$image_count = $this->input->post('image_count');
		
		$this->load->library('upload');
		
		$config['upload_path'] = UPLOAD_PATH;
		$config['allowed_types'] = '*';
		
		$image = array();
		for($i=1; $i<=$image_count; $i++) {
			$this->upload->initialize($config);
			
			$this->upload->do_upload('image_'.$i);
			if(!$this->upload->display_errors()) {
				array_push($image, $this->upload->data());
			}
		}
		
		$date = preg_replace('/\./', '-', $date);
		$this->admin_model->cover_write_action($title, $date, $font_type, $font_color, $thumb_view, $type, $link, $image);
		
		header('Location: '.base_url('admin/cover'));
	}
	
	public function cover_sort_action() {
		
		$id_sort_array = $this->input->post('id_sort_array');
		
		$this->admin_model->cover_sort_action($id_sort_array);
	}
	
	public function cover_delete_action() {
		
		$id = $this->input->post('id');
		
		$this->admin_model->cover_delete_action($id);
		
	}
	
	public function cover_edit($id, $limit, $offset) {
		
		$cover_view_row = $this->admin_model->cover_view($id);
		
		$cover_view_row['image'] = json_decode($cover_view_row['image'], true);
		
		$image_array = array();
		foreach($cover_view_row['image'] as $row) {
			$row['id'] = md5($row['file_name']);
			array_push($image_array, $row);
		}
		$cover_view_row['image'] = $image_array;
		
		$data['limit'] =  $limit;
		$data['offset'] =  $offset;
		
		$data['cover_view_row'] = $cover_view_row;
		
		$this->load->view('admin/cover_edit', $data);
	}
	
	public function cover_edit_action() {
		
		$id = $this->input->post('id');
		$title = $this->input->post('title');
		$date = $this->input->post('date');
		$font_type = $this->input->post('font_type');
		$font_color = $this->input->post('font_color');
		$thumb_view = $this->input->post('thumb_view');
		$type = $this->input->post('type');
		$link = $this->input->post('link');
		$image_count = $this->input->post('image_count');
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset');
		
		$cover_view_row = $this->admin_model->cover_view($id);
		$cover_view_row['image'] = json_decode($cover_view_row['image'], true);
		$image = array();
		foreach($cover_view_row['image'] as $row) {
			if($this->input->post(md5($row['file_name']))) {
				array_push($image, $row);
			}
		}
		
		$this->load->library('upload');
	
		$config['upload_path'] = UPLOAD_PATH;
		$config['allowed_types'] = '*';
	
		
		for($i=1; $i<=$image_count; $i++) {
			$this->upload->initialize($config);
				
			$this->upload->do_upload('image_'.$i);
			if(!$this->upload->display_errors()) {
				array_push($image, $this->upload->data());
			}
		}
	
		$date = preg_replace('/\./', '-', $date);
		$this->admin_model->cover_edit_action($id, $title, $date, $font_type, $font_color, $thumb_view, $type, $link, $image);
		
		header('Location: '.base_url('admin/cover/'.$limit.'/'.$offset));
	}
	
	public function main() {
		
		$main_rows = $this->admin_model->main();
		
		$sort_array = array();
		$main_rows_array = array();
		foreach($main_rows as $row) {
			array_push($sort_array, $row['sort']);
			
			$row['image_is_image'] = 0;
			$row['image_file_name'] = ';';
			$row['image_orig_name'] = '&nbsp;';
			if($row['view_kind'] == 'work') {
				$work_view_row = $this->admin_model->work_view($row['view_id']);
				$image = json_decode($work_view_row['image'], true);
				$row['image_count'] = count($image);
				if(!empty($row['image_count'])) {
					$row['image_is_image'] = $image[0]['is_image'];
					$row['image_file_name'] = $image[0]['file_name'];
					$row['image_orig_name'] = $image[0]['orig_name'];
				}
			}
			else if($row['view_kind'] == 'blog') {
				$blog_view_row = $this->admin_model->blog_view($row['view_id']);
				$image = json_decode($blog_view_row['image'], true);
				$row['image_count'] = count($image);
				if(!empty($row['image_count'])) {
					$row['image_is_image'] = $image[0]['is_image'];
					$row['image_file_name'] = $image[0]['file_name'];
					$row['image_orig_name'] = $image[0]['orig_name'];
				}
			}
			array_push($main_rows_array, $row);
		}
		$sort_string = implode(',', $sort_array);
		
		$data['main_rows'] =  $main_rows_array;
		$data['sort_string'] =  $sort_string;
		
		$this->load->view('admin/main', $data);
	}
	
	public function main_sort_action() {
	
		$id_sort_array = $this->input->post('id_sort_array');
	
		$this->admin_model->main_sort_action($id_sort_array);
	}
	
	public function main_view_work($limit = 10, $offset = 0) {
	
		$work_rows = $this->admin_model->work($limit, $offset);
	
		$total_rows = $this->admin_model->work_total_rows();
	
		$work_rows_array = array();
		foreach($work_rows as $row) {
			$image = json_decode($row['image'], true);
			$row['image_count'] = count($image);
			if(!empty($row['image_count'])) {
				$row['image_is_image'] = $image[0]['is_image'];
				$row['image_file_name'] = $image[0]['file_name'];
				$row['image_orig_name'] = $image[0]['orig_name'];
			} else {
				$row['image_is_image'] = 0;
				$row['image_file_name'] = ';';
				$row['image_orig_name'] = '&nbsp;';
			}
			
			if($row['date'] != '0000-00-00')  $row['date'] = date('M.Y', strtotime($row['date']));
			else $row['date'] = '';
			
			array_push($work_rows_array, $row);
		}
		
		$this->load->library('pagination');
	
		$config['base_url'] = "/view/view/view";
		$config['total_rows'] = $total_rows;
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><span>';
		$config['cur_tag_close'] = '</span></li>';
		$config['first_link'] = FALSE;
		$config['last_link'] = FALSE;
		$config['prev_link'] = 'Prev';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		$config['next_link'] = 'Next';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
	
		$this->pagination->initialize($config);
	
		$pagination_links =  $this->pagination->create_links();
		$pagination_links = preg_replace('/\/view\/view\/view\/(\d+)/', 'javascript:view_work($1);', $pagination_links);
		$pagination_links = preg_replace('/\/view\/view\/view\//', 'javascript:view_work(0);', $pagination_links);
		
		$data['limit'] =  $limit;
		$data['offset'] =  $offset;
		$data['work_rows'] =  $work_rows_array;
		$data['pagination_links'] =  $pagination_links;
	
		$json = json_encode($data);
		
		$data['json'] = $json;
		
		$this->load->view('json', $data);
	}
	
	public function main_view_blog($limit = 10, $offset = 0) {
	
		$blog_rows = $this->admin_model->blog($limit, $offset);
	
		$total_rows = $this->admin_model->blog_total_rows();
	
		$blog_rows_array = array();
		foreach($blog_rows as $row) {
			$image = json_decode($row['image'], true);
			$row['image_count'] = count($image);
			if(!empty($row['image_count'])) {
				$row['image_is_image'] = $image[0]['is_image'];
				$row['image_file_name'] = $image[0]['file_name'];
				$row['image_orig_name'] = $image[0]['orig_name'];
			} else {
				$row['image_is_image'] = 0;
				$row['image_file_name'] = ';';
				$row['image_orig_name'] = '&nbsp;';
			}
				
			if($row['date'] != '0000-00-00')  $row['date'] = date('M.Y', strtotime($row['date']));
			else $row['date'] = '';
				
			array_push($blog_rows_array, $row);
		}
	
		$this->load->library('pagination');
	
		$config['base_url'] = "/view/view/view";
		$config['total_rows'] = $total_rows;
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><span>';
		$config['cur_tag_close'] = '</span></li>';
		$config['first_link'] = FALSE;
		$config['last_link'] = FALSE;
		$config['prev_link'] = 'Prev';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		$config['next_link'] = 'Next';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
	
		$this->pagination->initialize($config);
	
		$pagination_links =  $this->pagination->create_links();
		$pagination_links = preg_replace('/\/view\/view\/view\/(\d+)/', 'javascript:view_blog($1);', $pagination_links);
		$pagination_links = preg_replace('/\/view\/view\/view\//', 'javascript:view_blog(0);', $pagination_links);
	
		$data['limit'] =  $limit;
		$data['offset'] =  $offset;
		$data['blog_rows'] =  $blog_rows_array;
		$data['pagination_links'] =  $pagination_links;
	
		$json = json_encode($data);
	
		$data['json'] = $json;
	
		$this->load->view('json', $data);
	}
	
	public function main_edit_action() {
	
		$id = $this->input->post('id');
		$view_kind = $this->input->post('view_kind');
		$view_id = $this->input->post('view_id');
	
		$this->admin_model->main_edit_action($id, $view_kind, $view_id);
	}
	
	public function category($limit = 10, $offset = 0) {
	
		$category_rows = $this->admin_model->category($limit, $offset);
	
		$total_rows = $this->admin_model->category_total_rows();
	
		$sort_array = array();
		foreach($category_rows as $row) {
			array_push($sort_array, $row['sort']);
		}
		$sort_string = implode(',', $sort_array);
	
		$this->load->library('pagination');
	
		$config['base_url'] = base_url(CONTROLLER.'/'.METHOD).'/'.$limit;
		$config['total_rows'] = $total_rows;
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><span>';
		$config['cur_tag_close'] = '</span></li>';
		$config['first_link'] = FALSE;
		$config['last_link'] = FALSE;
		$config['prev_link'] = 'Prev';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		$config['next_link'] = 'Next';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
	
		$this->pagination->initialize($config);
	
		$pagination_links =  $this->pagination->create_links();
	
		$data['limit'] =  $limit;
		$data['offset'] =  $offset;
		$data['category_rows'] =  $category_rows;
		$data['sort_string'] =  $sort_string;
		$data['pagination_links'] =  $pagination_links;
	
		$this->load->view('admin/category', $data);
	}
	
	public function category_write_action() {
	
		$title = $this->input->post('title');
		
		$this->admin_model->category_write_action($title);
	
		header('Location: '.base_url('admin/category'));
	}
	
	public function category_sort_action() {
	
		$id_sort_array = $this->input->post('id_sort_array');
	
		$this->admin_model->category_sort_action($id_sort_array);
	}
	
	public function category_delete_action() {
	
		$id = $this->input->post('id');
	
		$this->admin_model->category_delete_action($id);
	
	}
	
	public function category_view() {
	
		$id = $this->input->post('id');
	
		$category_view_row = $this->admin_model->category_view($id);
	
		$json = json_encode($category_view_row);
	
		$data['json'] = $json;
	
		$this->load->view('json', $data);
	}
	
	public function category_edit_action() {
	
		$id = $this->input->post('id');
		$title = $this->input->post('title');
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset');
	
		$this->admin_model->category_edit_action($id, $title);
	
		header('Location: '.base_url('admin/category/'.$limit.'/'.$offset));
	}
	
	public function work($limit = 10, $offset = 0) {
	
		$view_count = $this->admin_model->view_count('work');
		
		$work_rows = $this->admin_model->work($limit, $offset);
	
		$total_rows = $this->admin_model->work_total_rows();
	
		$sort_array = array();
		$work_rows_array = array();
		foreach($work_rows as $row) {
			array_push($sort_array, $row['sort']);
				
			$image = json_decode($row['image'], true);
			$row['image_count'] = count($image);
			if(!empty($row['image_count'])) {
				$row['image_is_image'] = $image[0]['is_image'];
				$row['image_file_name'] = $image[0]['file_name'];
				$row['image_orig_name'] = $image[0]['orig_name'];
			} else {
				$row['image_is_image'] = 0;
				$row['image_file_name'] = ';';
				$row['image_orig_name'] = '&nbsp;';
			}
			array_push($work_rows_array, $row);
		}
		$sort_string = implode(',', $sort_array);
	
		$this->load->library('pagination');
	
		$config['base_url'] = base_url(CONTROLLER.'/'.METHOD).'/'.$limit;
		$config['total_rows'] = $total_rows;
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><span>';
		$config['cur_tag_close'] = '</span></li>';
		$config['first_link'] = FALSE;
		$config['last_link'] = FALSE;
		$config['prev_link'] = 'Prev';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		$config['next_link'] = 'Next';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
	
		$this->pagination->initialize($config);
	
		$pagination_links =  $this->pagination->create_links();
	
		$data['limit'] =  $limit;
		$data['offset'] =  $offset;
		$data['work_view_count'] = $view_count;
		$data['work_rows'] =  $work_rows_array;
		$data['sort_string'] =  $sort_string;
		$data['pagination_links'] =  $pagination_links;
	
		$this->load->view('admin/work', $data);
	}
	
	public function work_write($limit, $offset) {
	
		$category_rows = $this->admin_model->category_rows();
		
		$data['limit'] =  $limit;
		$data['offset'] =  $offset;
		$data['category_rows'] = $category_rows;
	
		$this->load->view('admin/work_write', $data);
	}
	
	public function work_write_action() {
	
		$new_view = $this->input->post('new_view');
		$security = $this->input->post('security');
		$password = $this->input->post('password');
		$title1 = $this->input->post('title1');
		$title2 = $this->input->post('title2');
		$client = $this->input->post('client');
		$date = $this->input->post('date');
		$tag = $this->input->post('tag');
		$content = $this->input->post('content');
		$image_count = $this->input->post('image_count');
		$relation_id_1 = $this->input->post('relation_id_1');
		$relation_id_2 = $this->input->post('relation_id_2');
		$relation_id_3 = $this->input->post('relation_id_3');
		$relation_id_4 = $this->input->post('relation_id_4');
		
		$category_rows = $this->admin_model->category_rows();
		$category_id = array();
		foreach($category_rows as $row) {
			if($this->input->post('category_id_'.$row['id'])) {
				array_push($category_id, $row['id']);
			}
		}
		
		$this->load->library('upload');
	
		$config['upload_path'] = UPLOAD_PATH;
		$config['allowed_types'] = '*';
	
		$image = array();
		for($i=1; $i<=$image_count; $i++) {
			$this->upload->initialize($config);
				
			$this->upload->do_upload('image_'.$i);
			if(!$this->upload->display_errors()) {
				array_push($image, $this->upload->data());
			}
		}
	
		if(!empty($date)) $date = preg_replace('/\./', '-', $date).'-01';
		
		$this->admin_model->work_write_action($new_view, $security, $password, $title1, $title2, $client, $category_id,  $image, $date, $tag, $content, $relation_id_1, $relation_id_2, $relation_id_3, $relation_id_4);
	
		header('Location: '.base_url('admin/work'));
	}
	
	public function work_sort_action() {
	
		$id_sort_array = $this->input->post('id_sort_array');
	
		$this->admin_model->work_sort_action($id_sort_array);
	}
	
	public function work_delete_action() {
	
		$id = $this->input->post('id');
	
		$this->admin_model->work_delete_action($id);
	
	}
	
	public function work_edit($id, $limit, $offset) {
	
		$work_view_row = $this->admin_model->work_view($id);
	
		$category_rows = $this->admin_model->category_rows();
		
		$work_view_row['category_id'] = json_decode($work_view_row['category_id'], true);
		$work_view_row['image'] = json_decode($work_view_row['image'], true);
	
		$image_array = array();
		foreach($work_view_row['image'] as $row) {
			$row['id'] = md5($row['file_name']);
			array_push($image_array, $row);
		}
		$work_view_row['image'] = $image_array;
	
		$relation_array = array();
		$relation_id_array = array(
			array("name" => "relation_id_1", "id" => $work_view_row['relation_id_1']), 
			array("name" => "relation_id_2", "id" => $work_view_row['relation_id_2']), 
			array("name" => "relation_id_3", "id" => $work_view_row['relation_id_3']), 
			array("name" => "relation_id_4", "id" => $work_view_row['relation_id_4'])
			);
		foreach($relation_id_array as $row) {
			if(!empty($row['id'])) {
				$relation_work_view_row = $this->admin_model->work_view($row['id']);
				$image = json_decode($relation_work_view_row['image'], true);
				$row['image_count'] = count($image);
				if(!empty($row['image_count'])) {
					$row['image_is_image'] = $image[0]['is_image'];
					$row['image_file_name'] = $image[0]['file_name'];
					$row['image_orig_name'] = $image[0]['orig_name'];
				} else {
					$row['image_is_image'] = 0;
					$row['image_file_name'] = ';';
					$row['image_orig_name'] = '&nbsp;';
				}
			} else {
				$row['image_count'] = 0;
				$row['image_is_image'] = 0;
				$row['image_file_name'] = ';';
				$row['image_orig_name'] = '&nbsp;';
			}
			
			array_push($relation_array, $row);
		}
		$data['relation'] =  $relation_array;
		
		$data['limit'] =  $limit;
		$data['offset'] =  $offset;
		$data['category_rows'] = $category_rows;
		$data['work_view_row'] = $work_view_row;
		
		$this->load->view('admin/work_edit', $data);
	}
	
	public function work_edit_action() {
		
		$id = $this->input->post('id');
		$new_view = $this->input->post('new_view');
		$security = $this->input->post('security');
		$password = $this->input->post('password');
		$title1 = $this->input->post('title1');
		$title2 = $this->input->post('title2');
		$client = $this->input->post('client');
		$date = $this->input->post('date');
		$tag = $this->input->post('tag');
		$content = $this->input->post('content');
		$image_count = $this->input->post('image_count');
		$relation_id_1 = $this->input->post('relation_id_1');
		$relation_id_2 = $this->input->post('relation_id_2');
		$relation_id_3 = $this->input->post('relation_id_3');
		$relation_id_4 = $this->input->post('relation_id_4');
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset');
		
		$category_rows = $this->admin_model->category_rows();
		$category_id = array();
		foreach($category_rows as $row) {
			if($this->input->post('category_id_'.$row['id'])) {
				array_push($category_id, $row['id']);
			}
		}
		
		$work_view_row = $this->admin_model->work_view($id);
		$work_view_row['image'] = json_decode($work_view_row['image'], true);
		$image = array();
		foreach($work_view_row['image'] as $row) {
			if($this->input->post(md5($row['file_name']))) {
				array_push($image, $row);
			}
		}
		
		$this->load->library('upload');
	
		$config['upload_path'] = UPLOAD_PATH;
		$config['allowed_types'] = '*';
	
		for($i=1; $i<=$image_count; $i++) {
			$this->upload->initialize($config);
				
			$this->upload->do_upload('image_'.$i);
			if(!$this->upload->display_errors()) {
				array_push($image, $this->upload->data());
			}
		}
		
		if(!empty($date)) $date = preg_replace('/\./', '-', $date).'-01';
		$this->admin_model->work_edit_action($id, $new_view, $security, $password, $title1, $title2, $client, $category_id,  $image, $date, $tag, $content, $relation_id_1, $relation_id_2, $relation_id_3, $relation_id_4);
	
		header('Location: '.base_url('admin/work/'.$limit.'/'.$offset));
	}
	
	public function view_count_action() {
		
		$mode = $this->input->post('mode');
		$count = $this->input->post('count');
		
		$this->admin_model->view_count_action($mode, $count);
	}
	
	public function blog_category($limit = 10, $offset = 0) {
	
		$category_rows = $this->admin_model->blog_category($limit, $offset);
	
		$total_rows = $this->admin_model->blog_category_total_rows();
	
		$sort_array = array();
		foreach($category_rows as $row) {
			array_push($sort_array, $row['sort']);
		}
		$sort_string = implode(',', $sort_array);
	
		$this->load->library('pagination');
	
		$config['base_url'] = base_url(CONTROLLER.'/'.METHOD).'/'.$limit;
		$config['total_rows'] = $total_rows;
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><span>';
		$config['cur_tag_close'] = '</span></li>';
		$config['first_link'] = FALSE;
		$config['last_link'] = FALSE;
		$config['prev_link'] = 'Prev';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		$config['next_link'] = 'Next';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
	
		$this->pagination->initialize($config);
	
		$pagination_links =  $this->pagination->create_links();
	
		$data['limit'] =  $limit;
		$data['offset'] =  $offset;
		$data['category_rows'] =  $category_rows;
		$data['sort_string'] =  $sort_string;
		$data['pagination_links'] =  $pagination_links;
	
		$this->load->view('admin/blog_category', $data);
	}
	
	public function blog_category_write_action() {
	
		$title = $this->input->post('title');
	
		$this->admin_model->blog_category_write_action($title);
	
		header('Location: '.base_url('admin/blog_category'));
	}
	
	public function blog_category_sort_action() {
	
		$id_sort_array = $this->input->post('id_sort_array');
	
		$this->admin_model->blog_category_sort_action($id_sort_array);
	}
	
	public function blog_category_delete_action() {
	
		$id = $this->input->post('id');
	
		$this->admin_model->blog_category_delete_action($id);
	
	}
	
	public function blog_category_view() {
	
		$id = $this->input->post('id');
	
		$category_view_row = $this->admin_model->blog_category_view($id);
	
		$json = json_encode($category_view_row);
	
		$data['json'] = $json;
	
		$this->load->view('json', $data);
	}
	
	public function blog_category_edit_action() {
	
		$id = $this->input->post('id');
		$title = $this->input->post('title');
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset');
	
		$this->admin_model->blog_category_edit_action($id, $title);
	
		header('Location: '.base_url('admin/blog_category/'.$limit.'/'.$offset));
	}
	
	public function blog($limit = 10, $offset = 0) {
	
		$view_count = $this->admin_model->view_count('blog');
	
		$blog_rows = $this->admin_model->blog($limit, $offset);
	
		$total_rows = $this->admin_model->blog_total_rows();
	
		$sort_array = array();
		$blog_rows_array = array();
		foreach($blog_rows as $row) {
			array_push($sort_array, $row['sort']);
	
			$image = json_decode($row['image'], true);
			$row['image_count'] = count($image);
			if(!empty($row['image_count'])) {
				$row['image_is_image'] = $image[0]['is_image'];
				$row['image_file_name'] = $image[0]['file_name'];
				$row['image_orig_name'] = $image[0]['orig_name'];
			} else {
				$row['image_is_image'] = 0;
				$row['image_file_name'] = ';';
				$row['image_orig_name'] = '&nbsp;';
			}
			array_push($blog_rows_array, $row);
		}
		$sort_string = implode(',', $sort_array);
	
		$this->load->library('pagination');
	
		$config['base_url'] = base_url(CONTROLLER.'/'.METHOD).'/'.$limit;
		$config['total_rows'] = $total_rows;
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><span>';
		$config['cur_tag_close'] = '</span></li>';
		$config['first_link'] = FALSE;
		$config['last_link'] = FALSE;
		$config['prev_link'] = 'Prev';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		$config['next_link'] = 'Next';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
	
		$this->pagination->initialize($config);
	
		$pagination_links =  $this->pagination->create_links();
	
		$data['limit'] =  $limit;
		$data['offset'] =  $offset;
		$data['blog_view_count'] = $view_count;
		$data['blog_rows'] =  $blog_rows_array;
		$data['sort_string'] =  $sort_string;
		$data['pagination_links'] =  $pagination_links;
	
		$this->load->view('admin/blog', $data);
	}
	
	public function blog_write($limit, $offset) {
	
		$category_rows = $this->admin_model->blog_category_rows();
	
		$data['limit'] =  $limit;
		$data['offset'] =  $offset;
		$data['category_rows'] = $category_rows;
	
		$this->load->view('admin/blog_write', $data);
	}
	
	public function blog_write_action() {
	
		$title = $this->input->post('title');
		$date = $this->input->post('date');
		$client = $this->input->post('client');
		$content = $this->input->post('content');
		$image_count = $this->input->post('image_count');
	
		$category_rows = $this->admin_model->blog_category_rows();
		$category_id = array();
		foreach($category_rows as $row) {
			if($this->input->post('category_id_'.$row['id'])) {
				array_push($category_id, $row['id']);
			}
		}
	
		$this->load->library('upload');
	
		$config['upload_path'] = UPLOAD_PATH;
		$config['allowed_types'] = '*';
	
		$image = array();
		for($i=1; $i<=$image_count; $i++) {
			$this->upload->initialize($config);
	
			$this->upload->do_upload('image_'.$i);
			if(!$this->upload->display_errors()) {
				array_push($image, $this->upload->data());
			}
		}
	
		if(!empty($date)) $date = preg_replace('/\./', '-', $date).'-01';
	
		$this->admin_model->blog_write_action($title, $date, $client, $category_id,  $image, $content);
	
		header('Location: '.base_url('admin/blog'));
	}
	
	public function blog_sort_action() {
	
		$id_sort_array = $this->input->post('id_sort_array');
	
		$this->admin_model->blog_sort_action($id_sort_array);
	}
	
	public function blog_delete_action() {
	
		$id = $this->input->post('id');
	
		$this->admin_model->blog_delete_action($id);
	
	}
	
	public function blog_edit($id, $limit, $offset) {
	
		$blog_view_row = $this->admin_model->blog_view($id);
	
		$category_rows = $this->admin_model->blog_category_rows();
	
		$blog_view_row['category_id'] = json_decode($blog_view_row['category_id'], true);
		$blog_view_row['image'] = json_decode($blog_view_row['image'], true);
	
		$image_array = array();
		foreach($blog_view_row['image'] as $row) {
			$row['id'] = md5($row['file_name']);
			array_push($image_array, $row);
		}
		$blog_view_row['image'] = $image_array;
	
		$data['limit'] =  $limit;
		$data['offset'] =  $offset;
	
		$data['limit'] =  $limit;
		$data['offset'] =  $offset;
		$data['category_rows'] = $category_rows;
		$data['blog_view_row'] = $blog_view_row;
	
		$this->load->view('admin/blog_edit', $data);
	}
	
	public function blog_edit_action() {
	
		$id = $this->input->post('id');
		$title = $this->input->post('title');
		$date = $this->input->post('date');
		$client = $this->input->post('client');
		$content = $this->input->post('content');
		$image_count = $this->input->post('image_count');
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset');
		
		$category_rows = $this->admin_model->blog_category_rows();
		$category_id = array();
		foreach($category_rows as $row) {
			if($this->input->post('category_id_'.$row['id'])) {
				array_push($category_id, $row['id']);
			}
		}
	
		$blog_view_row = $this->admin_model->blog_view($id);
		$blog_view_row['image'] = json_decode($blog_view_row['image'], true);
		$image = array();
		foreach($blog_view_row['image'] as $row) {
			if($this->input->post(md5($row['file_name']))) {
				array_push($image, $row);
			}
		}
	
		$this->load->library('upload');
	
		$config['upload_path'] = UPLOAD_PATH;
		$config['allowed_types'] = '*';
	
		for($i=1; $i<=$image_count; $i++) {
			$this->upload->initialize($config);
	
			$this->upload->do_upload('image_'.$i);
			if(!$this->upload->display_errors()) {
				array_push($image, $this->upload->data());
			}
		}
	
		if(!empty($date)) $date = preg_replace('/\./', '-', $date).'-01';
		$this->admin_model->blog_edit_action($id, $title, $date, $client, $category_id, $image, $content);
	
		header('Location: '.base_url('admin/blog/'.$limit.'/'.$offset));
	}
	
	public function careers() {
	
		$view_count = $this->admin_model->view_count('careers');
	
		$careers_rows = $this->admin_model->careers();
	
		$data['careers_view_count'] = $view_count;
		$data['careers_rows'] =  $careers_rows;
		
		$this->load->view('admin/careers', $data);
	}
	
	public function careers_write() {
	
		$data = '';
		
		$this->load->view('admin/careers_write', $data);
	}
	
	public function careers_write_action() {
	
		$title = $this->input->post('title');
		$height = $this->input->post('height');
		$content = $this->input->post('content');
		
		$this->admin_model->careers_write_action($title, $height, $content);
	
		header('Location: '.base_url('admin/careers'));
	}
	
	public function careers_view_action() {
	
		$id = $this->input->post('id');
	
		$this->admin_model->careers_view_action($id);
	}
	
	public function careers_delete_action() {
	
		$id = $this->input->post('id');
	
		$this->admin_model->careers_delete_action($id);
	
	}
	
	public function careers_edit($id) {
	
		$careers_view_row = $this->admin_model->careers_view($id);
	
		$data['careers_view_row'] = $careers_view_row;
	
		$this->load->view('admin/careers_edit', $data);
	}
	
	public function careers_edit_action() {
	
		$id = $this->input->post('id');
		$title = $this->input->post('title');
		$height = $this->input->post('height');
		$content = $this->input->post('content');
		
		$this->admin_model->careers_edit_action($id, $title, $height, $content);
	
		header('Location: '.base_url('admin/careers'));
	}
	
	public function project_request($limit = 10, $offset = 0) {
	
		$project_request_rows = $this->admin_model->project_request($limit, $offset);
	
		$total_rows = $this->admin_model->project_request_total_rows();
	
		$this->load->library('pagination');
	
		$config['base_url'] = base_url(CONTROLLER.'/'.METHOD).'/'.$limit;
		$config['total_rows'] = $total_rows;
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><span>';
		$config['cur_tag_close'] = '</span></li>';
		$config['first_link'] = FALSE;
		$config['last_link'] = FALSE;
		$config['prev_link'] = 'Prev';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		$config['next_link'] = 'Next';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
	
		$this->pagination->initialize($config);
	
		$pagination_links =  $this->pagination->create_links();
	
		$data['limit'] =  $limit;
		$data['offset'] =  $offset;
		$data['project_request_rows'] =  $project_request_rows;
		$data['pagination_links'] =  $pagination_links;
	
		$this->load->view('admin/project_request', $data);
	}
	
	public function project_request_delete_action() {
	
		$id = $this->input->post('id');
	
		$this->admin_model->project_request_delete_action($id);
	
	}
	
	public function project_request_edit($id, $limit, $offset) {
	
		$project_request_view_row = $this->admin_model->project_request_view($id);
	
		$project_request_view_row['kind'] = json_decode($project_request_view_row['kind'], true);
		$project_request_view_row['image'] = json_decode($project_request_view_row['image'], true);
	
		$data['limit'] =  $limit;
		$data['offset'] =  $offset;
	
		$data['limit'] =  $limit;
		$data['offset'] =  $offset;
		$data['project_request_view_row'] = $project_request_view_row;
	
		$this->load->view('admin/project_request_edit', $data);
	}
	
	public function project_request_edit_action() {
	
		$id = $this->input->post('id');
		$company_name = $this->input->post('company_name');
		$user_name = $this->input->post('user_name');
		$email_address = $this->input->post('email_address');
		$phone_number = $this->input->post('phone_number');
		$film = $this->input->post('film');
		$motion_graphic = $this->input->post('motion_graphic');
		$ux = $this->input->post('ux');
		$website = $this->input->post('website');
		$etc = $this->input->post('etc');
		$budget = $this->input->post('budget');
		$schedule = $this->input->post('schedule');
		$comment = $this->input->post('comment');
		
		if(!$film) $film = 0;
		if(!$motion_graphic) $motion_graphic = 0;
		if(!$ux) $ux = 0;
		if(!$website) $website = 0;
		if(!$etc) $etc = 0;
		
		$kind = array(
				'film' => $film,
				'motion_graphic' => $motion_graphic,
				'ux' => $ux,
				'website' => $website,
				'etc' => $etc
				);
	
		$this->load->library('upload');
	
		$config['upload_path'] = PROJECT_REQUEST_UPLOAD_PATH;
		$config['allowed_types'] = '*';
	
		$image = array();
		$this->upload->initialize($config);
				
		$this->upload->do_upload('image');
		if(!$this->upload->display_errors()) {
			$image = $this->upload->data();
		}
	
		$this->admin_model->project_request_edit_action($id, $company_name, $user_name, $email_address, $phone_number, $kind, $budget, $schedule, $comment, $image);
	
		header('Location: '.base_url('admin/project_request/'.$limit.'/'.$offset));
	}
}

/* End of file admin.php */
/* Location: ./application/controllers/admin.php */